package com.cskaoyan.demo6.service;

import com.cskaoyan.demo6.bean.model.MarketCategory;
import com.cskaoyan.demo6.bean.model.MarketCategoryExample;
import com.cskaoyan.demo6.bean.vo.HomeIndexVo;
import com.cskaoyan.demo6.mapper.MarketCategoryMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/21 16:12
 */

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    GoodsService goodsService;

    @Override
    public List<MarketCategory> selectL1() {
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria exampleCriteria = categoryExample.createCriteria();
        exampleCriteria.andLevelEqualTo("L1");
        exampleCriteria.andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(categoryExample);
        return marketCategories;
    }

    @Override
    public List<HomeIndexVo.FloorGoodsListDTO> selectList(int i, Integer catlogListLimit, Integer catlogGoodsLimit) {
        List<HomeIndexVo.FloorGoodsListDTO> floorGoodsList = new ArrayList<>();

        // 一级类目
        PageHelper.offsetPage(i, catlogListLimit);
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria exampleCriteria = categoryExample.createCriteria();
        exampleCriteria.andLevelEqualTo("L1");
        exampleCriteria.andDeletedEqualTo(false);
        categoryExample.setOrderByClause("add_time desc");
        List<MarketCategory> level1 = marketCategoryMapper.selectByExample(categoryExample);

        // 二级类目
        MarketCategoryExample l2Example = new MarketCategoryExample();
        MarketCategoryExample.Criteria l2Criteria = categoryExample.createCriteria();
        exampleCriteria.andLevelEqualTo("L2");
        exampleCriteria.andDeletedEqualTo(false);
        List<MarketCategory> level2 = marketCategoryMapper.selectByExample(l2Example);
        // 把 l2 装到l1中
        Map<Integer, List<MarketCategory>> level2IdLevelMap = level2.stream().collect(Collectors.groupingBy(MarketCategory::getPid));
        level1.forEach(level->level.setChildren(level2IdLevelMap.get(level.getId())));

        // 查询对应的商品
        level1.forEach(level->{
            HomeIndexVo.FloorGoodsListDTO floorGoodsListDTO = new HomeIndexVo.FloorGoodsListDTO();
            floorGoodsListDTO.setId(level.getId());
            floorGoodsListDTO.setName(level.getName());
            List<HomeIndexVo.FloorGoodsListDTO.GoodsListDTO> goodsList = goodsService.selectGoodsListByCategory(level, catlogGoodsLimit);
            floorGoodsListDTO.setGoodsList(goodsList);
            floorGoodsList.add(floorGoodsListDTO);
        });


        return floorGoodsList;
    }
}

